﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;

namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsT_Man_ArticuloDAO
    {
        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();

        public List<T_MAN_ARTICULO> listarArticulos()
        {
            List<T_MAN_ARTICULO> listSistema = new List<T_MAN_ARTICULO>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAN_ARTICULO
                        where c.IND_ELIMINAR == 0
                        select c;
                listSistema = q.ToList();

            }
            catch (Exception)
            {

            }
            return listSistema;
        }







        
        public bool Insertar(T_MAN_ARTICULO Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("T_MAN_ARTICULO", Obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool Modificar(T_MAN_ARTICULO obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAN_ARTICULO", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }

        public bool Eliminar(T_MAN_SISTEMA obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAN_ARTICULO", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public int ObtenerIdSistema()
        {
            int id_opcion = 0;

            try
            {
                var q = (from c in ContextoDeObjs.T_MAN_SISTEMA.ToList()
                         select c.ID_SISTEMA).DefaultIfEmpty(0).Max();

                id_opcion = q;

                //id_opcion = lis[0].OPCION_ID;

            }
            catch (Exception)
            {
            }
            return id_opcion + 1;
        }





        //william




        public List<T_MAN_ARTICULO> listarArticulosLike(string cod_material,string nom_material_ingles)
        {
            List<T_MAN_ARTICULO> listSistema = new List<T_MAN_ARTICULO>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAN_ARTICULO
                        where c.IND_ELIMINAR == 0 && c.COD_MATERIAL.Contains(cod_material)&&c.NOM_MATERIAL_INGLES.Contains(nom_material_ingles)
                        select c;
                listSistema = q.ToList();

            }
            catch (Exception)
            {

            }
            return listSistema;
        }





        public List<T_MAN_ARTICULO> list_consulta(string cod_material) {


            List<T_MAN_ARTICULO> list = new List<T_MAN_ARTICULO>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAN_ARTICULO
                        where c.IND_ELIMINAR == 0 && c.COD_MATERIAL==cod_material
                        select c;
                list = q.ToList();

            }
            catch (Exception)
            {

            }
            return list;        
        }















        public IEnumerable<Object> listarT_Man_articulo_like(string des_material)
        {

            IEnumerable<Object> lista = new List<Object>();

            try
            {

                var q = (from tmmi in ContextoDeObjs.T_MAN_MODELO_IMAGEN
                        from tm in ContextoDeObjs.TD_MODELO
                        from tmsm in ContextoDeObjs.T_MAN_SISTEMA_MODELO
                        from tmsa in ContextoDeObjs.T_MAN_SISTEMA_ARTICULO
                        from tma in ContextoDeObjs.T_MAN_ARTICULO
                        where tmmi.ID_MODELO==tm.ID_MODELO && tm.ID_MODELO==tmsm.ID_MODELO && tmsm.ID_MODELO==tmsa.ID_MODELO
                        && tmsa.COD_MATERIAL==tma.COD_MATERIAL   && tma.NOM_MATERIAL_ESP.Contains(des_material)
                        select new
                        {
                            tmmi.DES_IMAGEN,
                            tm.DES_MODELO,
                            tm.COD_MODELO,
                            tm.ID_MODELO,
                            tm.DES_MARCA,
                            tm.TIP_NUM_RUEDA

                        }).Distinct();


                lista = q.ToList();


            }
            catch (Exception)
            {


            }

            return lista;

        }








    }
}
